package com.nanoso.dao.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.nanoso.dao.UrlDAO;
import com.nanoso.exception.DAOException;
import com.nanoso.module.Url;

public class UrlDAOImpl implements UrlDAO {

	private SqlMapClientTemplate sqlMapClientTemplate;

	public void setSqlMapClientTemplate(
			SqlMapClientTemplate sqlMapClientTemplate) {
		this.sqlMapClientTemplate = sqlMapClientTemplate;
	}

	@SuppressWarnings("unchecked")
	public List<Url> queryAllUrls() throws DAOException {

		return (List<Url>) this.sqlMapClientTemplate
				.queryForList("UrlDAO.queryAllUrls");
	}

	@SuppressWarnings("unchecked")
	public List<Url> queryUrlsByIds(long start, long end) throws DAOException {
		Map<String, Object> paras = new HashMap<String, Object>();
		paras.put("start", start);
		paras.put("end", end);
		return this.sqlMapClientTemplate.queryForList("UrlDAO.queryUrlsByIds",
				paras);
	}

	public Long queryMaxId() throws DAOException {

		return (Long) this.sqlMapClientTemplate
				.queryForObject("UrlDAO.queryMaxId");
	}

	public void addUrl(Url url) throws DAOException {

		this.sqlMapClientTemplate.insert("UrlDAO.addUrl", url);
	}

	public void updateUrl(Url url) throws DAOException {

		this.sqlMapClientTemplate.update("UrlDAO.updateUrl", url);
	}

	public Url queryUrlById(long id) throws DAOException {

		return (Url) this.sqlMapClientTemplate.queryForObject("UrlDAO.queryUrlById",
				id);
	}

}
